package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.PeopleFunc;

import java.util.List;

/**
 * @author Denis
 * @ClassName: PeopleFuncAdapter
 * @Description: 租客管理功能列表适配器
 * @date 2017/6/7 15:24
 */

public class PeopleFuncAdapter extends DelegateAdapter.Adapter<PeopleFuncAdapter.PeopleFuncHolder> {

    private Context context;
    private LayoutHelper helper;
    private List<PeopleFunc> datas;

    public PeopleFuncAdapter(Context context, LayoutHelper helper, List<PeopleFunc> datas) {
        this.context = context;
        this.helper = helper;
        this.datas = datas;
    }

    public void setDatas(List<PeopleFunc> datas) {
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return helper;
    }


    @Override
    public PeopleFuncHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_people_func, parent, false);
        return new PeopleFuncHolder(view);
    }

    @Override
    public void onBindViewHolder(PeopleFuncHolder holder, int position) {
        final PeopleFunc peopleFunc = datas.get(position);
        holder.ivIcon.setImageResource(peopleFunc.icon);
        holder.tvTitle.setText(peopleFunc.title);
        holder.tvDetail.setText(peopleFunc.detail);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(context, peopleFunc.className);
                    intent.putExtra("funcClassName", peopleFunc.funcClassName);
                    context.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    protected class PeopleFuncHolder extends RecyclerView.ViewHolder {
        public TextView tvTitle;
        public TextView tvDetail;
        public ImageView ivIcon;
        public ImageView ivGo;

        public PeopleFuncHolder(View itemView) {
            super(itemView);
            ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
            ivGo = (ImageView) itemView.findViewById(R.id.iv_go);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvDetail = (TextView) itemView.findViewById(R.id.tv_detail);
        }
    }
}
